{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "02bcd97d",
   "metadata": {},
   "source": [
    "# python网络爬虫应用实战（第20期）第3课书面作业\n",
    "学号：115799\n",
    "\n",
    "**作业内容：**  \n",
    "1. http://www.pythonscraping.com/pages/warandpeace.html  \n",
    "提取以上网页中所有的对话（显示为红色字体）\n",
    "\n",
    "2. http://www.pythonscraping.com/pages/page3.html  \n",
    "提取以上网页中礼物标题（title）和礼物花费（cost）  \n",
    "查询礼物“Dead Parrot” 的花费（cost）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f927f3c3",
   "metadata": {},
   "source": [
    "## 第1题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "362e15d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 :  Well, Prince, so Genoa and Lucca are now just family estates of the\n",
      "Buonapartes. But I warn you, if you don't tell me that this means war,\n",
      "if you still try to defend the infamies and horrors perpetrated by\n",
      "that Antichrist- I really believe he is Antichrist- I will have\n",
      "nothing more to do with you and you are no longer my friend, no longer\n",
      "my 'faithful slave,' as you call yourself! But how do you do? I see\n",
      "I have frightened you- sit down and tell me all the news.\n",
      "\n",
      "1 :  If you have nothing better to do, Count [or Prince], and if the\n",
      "prospect of spending an evening with a poor invalid is not too\n",
      "terrible, I shall be very charmed to see you tonight between 7 and 10-\n",
      "Annette Scherer.\n",
      "\n",
      "2 :  Heavens! what a virulent attack!\n",
      "\n",
      "3 :  First of all, dear friend, tell me how you are. Set your friend's\n",
      "mind at rest,\n",
      "\n",
      "4 :  Can one be well while suffering morally? Can one be calm in times\n",
      "like these if one has any feeling?\n",
      "\n",
      "5 :  You are\n",
      "staying the whole evening, I hope?\n",
      "\n",
      "6 :  And the fete at the English ambassador's? Today is Wednesday. I\n",
      "must put in an appearance there,\n",
      "\n",
      "7 :  My daughter is\n",
      "coming for me to take me there.\n",
      "\n",
      "8 :  I thought today's fete had been canceled. I confess all these\n",
      "festivities and fireworks are becoming wearisome.\n",
      "\n",
      "9 :  If they had known that you wished it, the entertainment would\n",
      "have been put off,\n",
      "\n",
      "10 :  Don't tease! Well, and what has been decided about Novosiltsev's\n",
      "dispatch? You know everything.\n",
      "\n",
      "11 :  What can one say about it?\n",
      "\n",
      "12 :  What has been decided? They have decided that\n",
      "Buonaparte has burnt his boats, and I believe that we are ready to\n",
      "burn ours.\n",
      "\n",
      "13 :  Oh, don't speak to me of Austria. Perhaps I don't understand\n",
      "things, but Austria never has wished, and does not wish, for war.\n",
      "She is betraying us! Russia alone must save Europe. Our gracious\n",
      "sovereign recognizes his high vocation and will be true to it. That is\n",
      "the one thing I have faith in! Our good and wonderful sovereign has to\n",
      "perform the noblest role on earth, and he is so virtuous and noble\n",
      "that God will not forsake him. He will fulfill his vocation and\n",
      "crush the hydra of revolution, which has become more terrible than\n",
      "ever in the person of this murderer and villain! We alone must\n",
      "avenge the blood of the just one.... Whom, I ask you, can we rely\n",
      "on?... England with her commercial spirit will not and cannot\n",
      "understand the Emperor Alexander's loftiness of soul. She has\n",
      "refused to evacuate Malta. She wanted to find, and still seeks, some\n",
      "secret motive in our actions. What answer did Novosiltsev get? None.\n",
      "The English have not understood and cannot understand the\n",
      "self-abnegation of our Emperor who wants nothing for himself, but only\n",
      "desires the good of mankind. And what have they promised? Nothing! And\n",
      "what little they have promised they will not perform! Prussia has\n",
      "always declared that Buonaparte is invincible, and that all Europe\n",
      "is powerless before him.... And I don't believe a word that Hardenburg\n",
      "says, or Haugwitz either. This famous Prussian neutrality is just a\n",
      "trap. I have faith only in God and the lofty destiny of our adored\n",
      "monarch. He will save Europe!\n",
      "\n",
      "14 :  I think,\n",
      "\n",
      "15 :  that if you had been\n",
      "sent instead of our dear Wintzingerode you would have captured the\n",
      "King of Prussia's consent by assault. You are so eloquent. Will you\n",
      "give me a cup of tea?\n",
      "\n",
      "16 :  In a moment. A propos,\n",
      "\n",
      "17 :  I am\n",
      "expecting two very interesting men tonight, le Vicomte de Mortemart,\n",
      "who is connected with the Montmorencys through the Rohans, one of\n",
      "the best French families. He is one of the genuine emigres, the good\n",
      "ones. And also the Abbe Morio. Do you know that profound thinker? He\n",
      "has been received by the Emperor. Had you heard?\n",
      "\n",
      "18 :  I shall be delighted to meet them,\n",
      "\n",
      "19 :  But tell me,\n",
      "\n",
      "20 :  is it true that the Dowager Empress wants Baron Funke\n",
      "to be appointed first secretary at Vienna? The baron by all accounts\n",
      "is a poor creature.\n",
      "\n",
      "21 :  Baron Funke has been recommended to the Dowager Empress by her\n",
      "sister,\n",
      "\n",
      "22 :  Now about your family. Do you know that since your daughter came\n",
      "out everyone has been enraptured by her? They say she is amazingly\n",
      "beautiful.\n",
      "\n",
      "23 :  I often think,\n",
      "\n",
      "24 :  I often think how unfairly sometimes the\n",
      "joys of life are distributed. Why has fate given you two such splendid\n",
      "children? I don't speak of Anatole, your youngest. I don't like\n",
      "him,\n",
      "\n",
      "25 :  Two such charming children. And really you appreciate\n",
      "them less than anyone, and so you don't deserve to have them.\n",
      "\n",
      "26 :  I can't help it,\n",
      "\n",
      "27 :  Lavater would have said I\n",
      "lack the bump of paternity.\n",
      "\n",
      "28 :  Don't joke; I mean to have a serious talk with you. Do you know I\n",
      "am dissatisfied with your younger son? Between ourselves\n",
      "\n",
      "29 :  he was mentioned at Her\n",
      "Majesty's and you were pitied....\n",
      "\n",
      "30 :  What would you have me do?\n",
      "\n",
      "31 :  You know I did all\n",
      "a father could for their education, and they have both turned out\n",
      "fools. Hippolyte is at least a quiet fool, but Anatole is an active\n",
      "one. That is the only difference between them.\n",
      "\n",
      "32 :  And why are children born to such men as you? If you were not a\n",
      "father there would be nothing I could reproach you with,\n",
      "\n",
      "33 :  I am your faithful slave and to you alone I can confess that my\n",
      "children are the bane of my life. It is the cross I have to bear. That\n",
      "is how I explain it to myself. It can't be helped!\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "url = 'http://www.pythonscraping.com/pages/warandpeace.html'\n",
    "headers = {\n",
    "    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'\n",
    "}\n",
    "response = requests.get(url, headers=headers)\n",
    "\n",
    "if response.status_code == 200:\n",
    "#     print(response.text)\n",
    "    soup = BeautifulSoup(response.text, 'lxml')\n",
    "    am = soup.find_all('span', {'class':'red'})\n",
    "    for i, c in enumerate(am):\n",
    "        print(i,': ', c.get_text())\n",
    "        print('')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db0e0697",
   "metadata": {},
   "source": [
    "## 第2题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "afa58827",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dead Parrot $15.00\n",
      "Dead Parrot $10,000.52\n",
      "Dead Parrot $10,005.00\n",
      "Dead Parrot $0.50\n",
      "Dead Parrot $1.50\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "url = 'http://www.pythonscraping.com/pages/page3.html'\n",
    "headers = {\n",
    "    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'\n",
    "}\n",
    "title = 'Dead Parrot'\n",
    "response = requests.get(url, headers=headers)\n",
    "\n",
    "if response.status_code == 200:\n",
    "    soup = BeautifulSoup(response.text, 'lxml')\n",
    "    rows = soup.find('table', {\"id\":'giftList'}).find_all('tr')\n",
    "    for row in rows:\n",
    "        cells = row.find_all('td')\n",
    "        cost = ''\n",
    "        for i, c in enumerate(cells):\n",
    "            if i==0:\n",
    "                if c.get_text()!=title:\n",
    "                    continue\n",
    "            elif i==2:\n",
    "                cost = c.get_text().strip()\n",
    "                print(title, cost)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f7b86e3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
